package com.yjf.ljr.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;

import com.yjf.ljr.R;
import com.yjf.ljr.application.MyApplication;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.util.SPreferences;

/**
 * @author dongxie
 */

@SuppressWarnings("deprecation")
public class GroupActivity extends TabActivity {

	public static final String TAG = GroupActivity.class.getSimpleName();
	private static final String HOME = "主页";
	private static final String INVEST = "投资";
	private static final String ACCOUNT = "账户";
	private static final String MORE = "更多";

	private long mExitTime = 0;
	private static TabHost mTabHost;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
						| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		setContentView(R.layout.activity_group);// 设置TabHost使用的布局文件
		mTabHost = this.getTabHost();
		mTabHost.setFocusable(true);
		setupIntent();

		// 开启屏幕息屏事件监听服务
		// Intent lockPatternService = new
		// Intent(GroupActivity.this,LockPatternService.class);
		// startService(lockPatternService);

		// // 标签切换事件处理，setOnTabChangedListener
		// mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
		// @Override
		// public void onTabChanged(String tabId) {
		// setTabTextColor(mTabHost.getCurrentTab());
		// }
		// });
		// setTabTextColor(0);
	}

	public static void changeTab(int id) {
		mTabHost.setCurrentTab(id);
	}

	private void setupIntent() {
		mTabHost.addTab(buildTabSpec(HOME, R.drawable.homepg_home_selector, new Intent(this, HomePageActivity.class)));
		mTabHost.addTab(buildTabSpec(INVEST, R.drawable.homepg_invest_selector, new Intent(this,
				InvestListActivity.class)));
		mTabHost.addTab(buildTabSpec(ACCOUNT, R.drawable.homepg_account_selector, new Intent(this,
				MyAccountActivity.class)));
		mTabHost.addTab(buildTabSpec(MORE, R.drawable.homepg_more_selector, new Intent(this, MoreActivity.class)));
	}

	private TabSpec buildTabSpec(String tag, int icon, Intent intent) {
		View view = View.inflate(this, R.layout.item_tab, null);
		((ImageView) view.findViewById(R.id.iv_tab_icon)).setImageResource(icon);
		((TextView) view.findViewById(R.id.tv_tab_text)).setText(tag);
		return mTabHost.newTabSpec(tag).setIndicator(view).setContent(intent);
	}

	private void setTabTextColor(int id) {
		int count = mTabHost.getTabWidget().getChildCount();
		for (int i = 0; i < count; i++) {
			TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(R.id.tv_tab_text);
			if (id == i) {
				tv.setTextColor(Color.parseColor("#EF8612"));
			} else {
				tv.setTextColor(Color.parseColor("#9E9E9E"));
			}
		}
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		switch (event.getKeyCode()) {
		case KeyEvent.KEYCODE_BACK:
			if (event.getAction() == KeyEvent.ACTION_DOWN) {
				if (System.currentTimeMillis() - mExitTime < 2000) {
					CommonData.IS_LOGIN = false;
					// Intent lockPatternService = new
					// Intent(GroupActivity.this,LockPatternService.class);
					// stopService(lockPatternService);
					MyAccountActivity.gIsLoginServer = false;
					CommonData.SESSIONID = "";
					/** 清楚CookieManager中的cookie */
					CookieSyncManager.createInstance(GroupActivity.this);
					CookieManager cookieManager = CookieManager.getInstance();
					cookieManager.removeSessionCookie();
					CookieSyncManager.getInstance().sync();
					CommonData.FLAG_LOGIN_SERVER = false;
					CommonData.IS_LOGIN = false;
					CommonData.USER_NAME = "";
					SPreferences.clearPassword(GroupActivity.this);
					((MyApplication) getApplication()).stopAutoLogin();
					finish();
				} else {
					Toast.makeText(this, "再点一次将退出程序！", Toast.LENGTH_SHORT).show();
					mExitTime = System.currentTimeMillis();
				}
				return true;
			}
		}
		return super.dispatchKeyEvent(event);
	}

}